var result = false;

//验证手机号
var checkPhone = function(a) {
	var patrn = /^((?:13|15|18|14|17)\d{9}|0(?:10|2\d|[3-9]\d{2})[1-9]\d{6,7})$/;
	if(!patrn.exec(a)) return false;
	return true;
};

//判断前三个形势里程
function mileage(el, img) {
	el.length > 0 ? img.attr("src", "../images/car-icon1.png") : img.attr("src", "../images/index-icon.png");
	el.length > 0 ? Flag = true : Flag = false;
};

//图形验证
window.callback = function(res) {
	var loanTelphone = $('#loanTelphone').val();

	if(res.ret === 0) {

		$.ajax({
			async: true,

			type: "post",
			data: {
				ticket: res.ticket,
				randstr: res.randstr,
				phone: loanTelphone
			},
			url: "/vehicleassess/sendMsg",
			success: function(data) {
				console.log(data);
				if(data.code === 1001) {
					result = true;
					settime();
				} else {
					layer.msg(data.msg);
				}

			},
			error: function(error) {
				console.log(error);
			}

		});

	}
};

//点击下一步
$('.index-sure').on('click', function() {

	var patten = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
	var patten1 = /^\d{4}-((0([1-9]))|(1(0|1|2)))$/;
	var Oimg = $('.index-ident label img');
	var time = $('#car-time').val();
	var num = $('#car-mileage').val();
	if($(".ident-brand option:selected").text() == '直接选择或搜索选择') {
		layer.msg('请选择车辆品牌！');
	} else {
		if($(".ident-system option:selected").text() == '请选择') {
			layer.msg('请选择车系！');
		} else {

			if($(".ident-type option:selected").text() == '请选择') {
				layer.msg('请选择车型！');
			} else {
				if($(".ident-city option:selected").text() == '请选择') {
					layer.msg('请选择所在城市！');
				} else {
					if(!patten1.test(time)) {
						layer.msg('请选择正确的上牌时间！');
					} else {
						if(!patten.test(num)) {
							layer.msg('请输入正确的行驶里程数！');
						} else {

							$('.mask').css('visibility', 'visible');
							$('.index').css('filter', 'blur(5px)');
						}
					}
				}
			}
		}
	}

});

function myFunction() {
	myVar = setTimeout(function() {
		window.location.href = "projectCar.html";
	}, 2500);
};

function myStopFunction() {
	clearTimeout(myVar);
};
//提交车辆估值
function addInfo(data) {
	//		var barnd = $(".ident-brand option:selected").text();
	//		var series = $(".ident-system option:selected").text();
	//		var type = $(".ident-type option:selected").text();
	//	
	//		$.ajax({
	//			async: true,
	//	
	//			type: "post",
	//			data: {
	//				barnd:barnd ,
	//				series series,
	//				type: type,
	//				upTime: time,
	//				kmNum: num,
	//				cost: data.,
	//				b2c: c2b: c2c:
	//			},
	//			url: "vehicleassess/addInfo",
	//	
	//			success: function(data) {
	//				if(data.code === "10000") {
	//					addInfo();
	//				}
	//	
	//			}
	//	
	//		});
}
//进入手机号验证，验证码验证
$('.submit').on('click', function() {
	var loanTelphone = $('#loanTelphone').val();

	var loanShortYan = $('#loanShortYan').val();

	var autohomeid = $(".ident-type option:selected")[0].id;
	var time = $('#car-time').val();
	var num = $('#car-mileage').val();

	if(!checkPhone(loanTelphone)) {
		layer.msg('请输入正确的手机号！');
	} else {
		if(loanShortYan == '' || result == false) {
			layer.msg('点击获取验证码');
		} else {

			//判断验证码是否正确
			$.ajax({
				async: true,

				type: "post",
				data: {
					code: loanShortYan
				},
				url: "/vehicleassess/verification",
				success: function(data) {
					console.log(data);
					if(data.code === 1001) {

						//验证码正确进行总的估价
						$.ajax({
							async: true,

							type: "get",
							data: {
								appkey: 'bf8eb0d0d07466107f5c8a2837d729de',
								autohomeid: autohomeid,
								regdate: time,
								province: '23',
								city: '385',
								miles: num
							},
							url: "/guguiche/SeniorValuation",

							success: function(data) {
								console.log(data);
								if(data.code === "10000") {
									addInfo(data);
								}

							}

						});

					} else {
						layer.msg(data.msg);
					}

				},
				error: function(error) {
					console.log(error);
				}

			});
			//		
			//		//验证码正确进行总的估价

		}

	}

});
//遮罩取消
$(".cancel").on('click', function() {
	$('.mask').css('visibility', 'hidden');
	$('.index').css('filter', 'blur(0px)');

});

function onChange1(el, img) {
	$('.ident-type').empty();
	var systemId = $(".ident-system option:selected")[0].id;

	//请求车系对应的车型列表
	$.ajax({
		async: true,

		type: "get",
		data: {
			appkey: 'bf8eb0d0d07466107f5c8a2837d729de',
			familyid: systemId
		},
		url: "/guguiche/dmpdetailVehicleInfo",
		success: function(data) {

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;
				var str = {
					salesdesc: "请选择"
				};
				arror.unshift(str);

				for(var i = 0; i < arror.length; i++) {
					//								
					var html = "<option id=" + arror[i].autohomeid + " >" + arror[i].salesdesc + "</option>";
					$('.ident-type').append(html);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();

					});

				}

			}

		}

	});
}
//			//选择品牌时选择对应的车系
function onChange() {

	var id = $(".ident-brand option:selected")[0].id;
	$('.ident-system').empty();

	//发送请求获取对应车系
	$.ajax({
		async: true,

		type: "get",
		data: {
			appkey: 'bf8eb0d0d07466107f5c8a2837d729de',
			brandid: id
		},
		url: "/guguiche/dmpfamilyInfo",
		success: function(data) {

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;
				var str = {
					familyname: "请选择"
				};
				arror.unshift(str);

				for(var i = 0; i < arror.length; i++) {
					//								
					var html = "<option id=" + id + " >" + arror[i].familyname + "</option>";
					$('.ident-system').append(html);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();
						form.on('select(system)', function(data) {

							onChange1();
							data.value != '请选择' ? $('.car-system').attr("src", "../images/car-icon1.png") : $('.car-system').attr("src", "../images/index-icon.png");

						})
					});

				}

			}

		}

	});

}
//车辆品牌
$(function() {
	layui.use('form', function() { //此段代码必不可少
		var form = layui.form;
		form.render();
	});
	$.ajax({
		async: true,

		type: "get",
		data: {
			appkey: 'bf8eb0d0d07466107f5c8a2837d729de'
		},
		url: "/guguiche/dmpAllBrands",
		success: function(data) {

			if(data.result.result.code == 200) {

				var arror = data.result.result.result;

				arror.forEach(function(i, item) {
					var htmls = "";

					htmls += "<option id=" + i.id + ">" + i.brandname + "</option>";
					$('.ident-brand').append(htmls);
					layui.use('form', function() { //此段代码必不可少
						var form = layui.form;
						form.render();
						form.on('select(business)', function(data) {

							data.value != '请选择' && '直接选择或搜索选择' ? $('.car-brand').attr("src", "../images/car-icon1.png") : $('.car-brand').attr("src", "../images/index-icon.png");
							onChange();

						});
					});

				});

			}

		}

	});

});